//
//  WKPopupMenu.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/18.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKPopupMenu.h"

@interface WKPopupMenu ()
{
    UIView *_contentView;
}
@property(nonatomic,copy)void(^selectBlock)(NSInteger index);
@property(nonatomic,copy)void(^cancelBlock)(void);
@end

@implementation WKPopupMenu

+ (void)showWKPopupMenuCompletion:(void(^)(NSInteger index))completion cancletion:(void(^)(void))cancletion{
    WKPopupMenu *popupMenu = [[WKPopupMenu alloc]initWithFrame:ScreenBounds];
    [popupMenu show];
    popupMenu.selectBlock = completion;
    popupMenu.cancelBlock = cancletion;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        [self setup];
        
    }
    return self;
}

- (instancetype)init{
    if (self == [super init]) {
        self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        [self setup];
    }
    return self;
}

- (void)setup{
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, (227+HS_TabbarSafeBottomMargin))];
    contentView.backgroundColor = [UIColor whiteColor];
    [contentView wh_topCornerWithRadius:5];
    _contentView = contentView;
    [self addSubview:_contentView];
    NSArray *titleArr = @[@"匹配好友",@"发美说",@"发跟拍",@"提问题"];
    CGFloat imgWidth = 63;
    CGFloat cell_width = ScreenWidth/4;
    CGFloat cell_height =135;
    for (int i = 0; i<4; i++) {
        
        UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake(i*(cell_width), 0, cell_width, cell_height)];
        itemView.backgroundColor = [UIColor whiteColor];
         [_contentView addSubview:itemView];
        
        UIImageView *imgView = [[UIImageView alloc]init];
        imgView.frame = CGRectMake((cell_width-imgWidth)/2, 40, imgWidth, imgWidth);
        imgView.backgroundColor = [UIColor magentaColor];
        imgView.layer.masksToBounds = YES;
        imgView.layer.cornerRadius = imgWidth/2;
        [itemView addSubview:imgView];
        
        UILabel *titlelb = [[UILabel alloc]initWithFrame:CGRectMake((cell_width-imgWidth)/2, 120, imgWidth, 14)];
        titlelb.text = titleArr[i];
        titlelb.textColor = UIColorFromHex(0x3C3C3C);
        titlelb.textAlignment = NSTextAlignmentCenter;
        titlelb.font = PFSC_MediumFont(15);
        [itemView addSubview:titlelb];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*(cell_width), 0, cell_width, cell_height);
        btn.backgroundColor = [UIColor clearColor];
        btn.tag = 900+i;
        [btn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:btn];
        
    }

    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(((ScreenWidth-40)/2), 170, 40, 40);
    cancelBtn.backgroundColor = [UIColor redColor];
    [cancelBtn addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:cancelBtn];
    
}

#pragma mark -选择
- (void)selectAction:(UIButton*)sender{
    [self hidden];
    if (self.selectBlock) {
        self.selectBlock(sender.tag-900);
    }
    
}

- (void)show{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    keyWindow.backgroundColor = [UIColor clearColor];
    self.translatesAutoresizingMaskIntoConstraints = NO;
    [keyWindow addSubview:self];
    NSArray *view_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[self]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self)];
    NSArray *view_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[self]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(self)];
    [keyWindow addConstraints:view_H];
    [keyWindow addConstraints:view_V];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidden)];
    [self addGestureRecognizer:tap];
    
    _contentView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, (227+HS_TabbarSafeBottomMargin));
    [UIView animateWithDuration:0.2f animations:^{
        _contentView.frame = CGRectMake(0, ScreenHeight-(HS_TabbarSafeBottomMargin+227), ScreenWidth, (227+HS_TabbarSafeBottomMargin));
    }];

}

- (void)hidden{
    
    [UIView animateWithDuration:0.2f animations:^{
        _contentView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, (227+HS_TabbarSafeBottomMargin));
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }];
}

@end
